package thread;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-05-12
 * Time：23:45
 */
public class Demo16 {
    //创建一个对象作为锁对象
    public static void main(String[] args) {
        Object locker = new Object();
        //创建等待线程
        Thread waitThread = new Thread(() -> {
            synchronized (locker) {
                System.out.println("wait开始");
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("wait结束");
            }
        });

        //创建一个用来通知的线程
        Thread notifyThread = new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入任意内容，开始通知");
            scanner.next();
            synchronized (locker) {
                System.out.println("notify开始");
                locker.notify();
                System.out.println("notify结束");
            }
        });

        waitThread.start();
        notifyThread.start();
    }
}
